import 'package:flutter/material.dart';
import 'package:flutter_2b_client/utils/local_datetime.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

class DateRangeTextField extends HookWidget {
  final DateTimeRange? range;
  final Function(DateTimeRange? range) onChange;
  final String? placeholder;
  final DateTimeRange selectableRange;

  const DateRangeTextField({Key? key, this.range, required this.onChange,this.placeholder,required this.selectableRange})
      : super(key: key);


  @override
  Widget build(BuildContext context) {
    var textEditingController = useTextEditingController();
    useEffect(() {
      if (range == null) {
        textEditingController.text = "";
      } else {
        textEditingController.text =
            "${LocalDate(range!.start).format()} 至 ${LocalDate(range!.end).format()}";
      }
    }, [range]);
    return TextField(
      readOnly: true,
      controller: textEditingController,
      onTap: () async {
        var dateTimeRange = await showDateRangePicker(
          context: context,
          initialDateRange: range,
          firstDate: range?.start ?? DateTime.now(),
          lastDate: range?.end ?? DateTime.now(),
          locale: const Locale('zh'),
        );
        onChange(dateTimeRange);
      },
      decoration: InputDecoration(
        suffix: IconButton(
          onPressed: () {
            onChange(null);
          },
          icon: const Icon(Icons.clear),
        ),
        hintText: placeholder??"选择日期范围",
      ),
    );
  }
}
